//
//  DetailShopViewController.m
//  L7_test
//
//  Created by Mac on 11-12-29.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//
#import "DetailShopViewController.h"
#import "SectionHeaderController.h"
#import "SectionFooterController.h"
#import "ShopMenuController.h"
#import "SupplierManager.h"
#import "FMDBCollectionManager.h"
#import "Collection.h"
#import "FMDBTransactionHistory.h"
@implementation DetailShopViewController
@synthesize firstSectionArray;
@synthesize secondSectionArray;

- (void)viewDidLoad {
    //设置视图title
    self.title=@"商户详情";
    //定义导航栏左侧按钮
    UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0,0,54,27)];
    [leftButton setBackgroundImage:[UIImage imageNamed:@"Detail-002.png"] forState:UIControlStateNormal];
    [leftButton setBackgroundImage:[UIImage imageNamed:@"Detail-002.png"] forState:UIControlStateHighlighted];
    [leftButton setTitle:@"返 回" forState:UIControlStateNormal];
    leftButton.titleLabel.font=[UIFont fontWithName:@"Helvetica-Bold" size:14]; 
    [leftButton addTarget:self action:@selector(backButtonPressd) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    //设置为左边按钮
    [[self navigationItem] setLeftBarButtonItem:leftItem];
    [leftButton release];
    [leftItem release];
    //定义导航栏右侧按钮
    UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 54, 27)];
    [rightButton setBackgroundImage:[UIImage imageNamed:@"Detail-003.png"] forState:UIControlStateNormal];
    [rightButton setBackgroundImage:[UIImage imageNamed:@"Detail-003.png"] forState:UIControlStateHighlighted];
    [rightButton setTitle:@"菜 单" forState:UIControlStateNormal];
    rightButton.titleLabel.font = [UIFont fontWithName:@"Hevetica-Bold" size:14];
    [rightButton addTarget:self action:@selector(nextButtonPressd) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    //设置为右边按钮
    [[self navigationItem] setRightBarButtonItem:rightItem];
    [rightButton release];
    [rightItem release];
    
    //修改导航栏背景
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"001s.png"] forBarMetrics: UIBarMetricsDefault];
    
    supplier = [recieveDictionary objectForKey:@"Supplier"];
    
    //添加进入最近浏览
    FMDBTransactionHistory *fmdbTransactionHistory=[[FMDBTransactionHistory alloc]init];
    [fmdbTransactionHistory insertHistory:supplier];
    [fmdbTransactionHistory release];
    
    NSString *address = [NSString stringWithFormat:@"地址：%@",supplier.address];
    NSString *phone = [NSString stringWithFormat:@"电话：%@",supplier.phone];
    NSString *leastDistance = [NSString stringWithFormat:@"送餐范围：%0.1f 公里内",supplier.leastDistance]; 
    NSString *leastPrice = [NSString stringWithFormat:@"起送金额：¥ %0.2f 元",supplier.leastPrice]; 
    NSString *onTime = [NSString stringWithFormat:@"送餐时间：%d :00- %d :00",supplier.startTime,supplier.endTime]; 
    NSString *deliveryDesc = [NSString stringWithFormat:@"送餐说明：%@",supplier.deliveryDesc]; 
    
    NSArray *tmpFirstArray = [[NSArray alloc] initWithObjects:address,phone, nil];
    self.firstSectionArray = tmpFirstArray;
    [tmpFirstArray release];
    
    NSArray *tmpSecondArray = [[NSArray alloc] initWithObjects:leastDistance, leastPrice,onTime, deliveryDesc, nil];
    self.secondSectionArray = tmpSecondArray;
    [tmpSecondArray release];

    [super viewDidLoad];
}

- (void)backButtonPressd{
    //将当前视图推出栈，程序将进入上一视图
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)nextButtonPressd{
    ShopMenuController *shopMenuController = [[ShopMenuController alloc]initWithNibName:@"ShopMenuController" bundle:nil];
    UINavigationController  *theNavController = [[UINavigationController alloc]
                                                 initWithRootViewController:shopMenuController];
    NSString *supId = (NSString *)[recieveDictionary objectForKey:@"supId"];
    NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithCapacity :1];
    [dictionary setObject:supId forKey:@"supId"];
    [dictionary setObject:supplier.supplierName forKey:@"supName"];
    
    [shopMenuController setRecieveDictionary:dictionary];
    [shopMenuController release];
    theNavController.navigationBar.tintColor = [UIColor blackColor];
    [self.navigationController pushViewController: shopMenuController animated:YES];
    [theNavController release];
}

- (void)viewWillAppear:(BOOL)animated{
    //设置显示导航栏
    self.navigationController.navigationBarHidden=NO; 
}

//设置cell的高度
- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath == 0){
        return 45;
    }
    else{
        return 33;
    }
}

//设置为2个section
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

//设置section的行数
- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section {
    NSInteger rows = 0;
    switch (section) {
        case 0:
            rows = [firstSectionArray count];
            break;
        case 1:
            rows = [secondSectionArray count];
            break;
        default:
            break;
    }
    return rows;
}

//设置每个cell显示的内容
- (UITableViewCell *)tableView:(UITableView *)tableView
		 cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    //设置tableview的背景颜色为白色
    [tableView setBackgroundColor:[UIColor clearColor]];
    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
							 SimpleTableIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc]
				 initWithStyle:UITableViewCellStyleDefault
				 reuseIdentifier:SimpleTableIdentifier] autorelease];
        //设置cell的背景颜色为白色
        [cell setBackgroundColor:[UIColor clearColor]];
    }
    NSString *cellText = nil;
    switch (indexPath.section) {
        case 0:
            cellText = [firstSectionArray objectAtIndex:indexPath.row];
            break;
        case 1:
            cellText = [secondSectionArray objectAtIndex:indexPath.row];
            break;
        default:
            break;
    }
    cell.textLabel.text = cellText;
    cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:14];
    //在电话的cell后加上拨打电话的按钮
    if(indexPath.section == 0 && indexPath.row == 1){
        UIButton *phoneButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
        [phoneButton setFrame:CGRectMake(245, 0, 30.5, 32)];
        UIImage *image = [UIImage imageNamed:@"Detail-022.png"]; 
        [phoneButton setBackgroundImage:image forState:UIControlStateNormal]; 
        phoneButton.tag = 300;
        [cell.contentView addSubview:phoneButton];
        //点击拨号按钮直接拨号 
        [phoneButton addTarget:self action:@selector(callAction:event:) forControlEvents:UIControlEventTouchUpInside];
    }
    return cell;
}

//定义点击拨号按钮时的操作 
- (void)callAction:(id)sender event:(id)event{ 
    //phoneNumber为号码字符串 
    NSString *phoneNumber = [NSString stringWithFormat:@"tel://%@",supplier.phone]; 
    //拨号 
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
}

//设置section的header的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if(section==0){
        return 110;    
    }
    else{
        return 5;
    }
}

//加入收藏方法
- (IBAction)addCollectionPressd:(id)sender{
    FMDBCollectionManager *fmdbCollectionManager=[[FMDBCollectionManager alloc]init];
    BOOL isExist=[fmdbCollectionManager insertCollection:supplier];
    [fmdbCollectionManager getAllCollection];
    if(isExist){
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"收藏成功" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [alertView show];
        [alertView release];
    }else{
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"该商家已加入收藏" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [alertView show];
        [alertView release];
    }
    [fmdbCollectionManager release];
}

//设置section的header
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    //设置每组的的标题
    if (section == 0)
    {
        //用代码生成图片
        //CGRectMake(x,x,x,x)参数代表x,y,width,height。屏幕一般为320*460
        UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 60)] autorelease];
        UIImageView *backGroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(9, 9.5, 85,85)];
        backGroundImageView.image = [UIImage imageNamed:@"Detail-006.png"];
        UIImageView *headerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(16, 15.5, 71,71)];
        headerImageView.image = [UIImage imageNamed:@"Detail-007.png"];
        //用代码生成lbl
        UILabel *headerLab = [[UILabel alloc] initWithFrame:CGRectMake(105, 15.5, 200, 20)];
        headerLab.font = [UIFont fontWithName:@"Helvetica-Bold" size:18];
        headerLab.text = supplier.supplierName;
        
        //设置看菜单按钮
        UIButton *showMenuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        showMenuBtn.frame = CGRectMake(105, 56.5, 99.5, 29.5);
        [showMenuBtn setBackgroundImage:[UIImage imageNamed:@"Detail-016.png"] forState:UIControlStateNormal];
        [showMenuBtn setBackgroundImage:[UIImage imageNamed:@"Detail-016.png"] forState:UIControlStateHighlighted];
        [showMenuBtn setTitle:@"看菜单" forState:UIControlStateNormal];
        showMenuBtn.titleLabel.font=[UIFont fontWithName:@"Helvetica-Bold" size:16]; 
        [showMenuBtn addTarget:self action:@selector(nextButtonPressd) forControlEvents:UIControlEventTouchUpInside];
        
        //设置添加收藏按钮
        UIButton *addCollectionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        addCollectionBtn.frame = CGRectMake(212, 56.5, 99.5, 29.5);
        [addCollectionBtn setBackgroundImage:[UIImage imageNamed:@"Detail-016.png"] forState:UIControlStateNormal];
        [addCollectionBtn setBackgroundImage:[UIImage imageNamed:@"Detail-016.png"] forState:UIControlStateHighlighted];
        [addCollectionBtn setTitle:@"加入收藏" forState:UIControlStateNormal];
        addCollectionBtn.titleLabel.font=[UIFont fontWithName:@"Helvetica-Bold" size:16]; 
        [addCollectionBtn addTarget:self action:@selector(addCollectionPressd:) forControlEvents:UIControlEventTouchUpInside];
        
        //添加到视图
        [headerView addSubview:backGroundImageView];
        [headerView addSubview:headerImageView];
        [headerView addSubview:headerLab];
        [headerView addSubview:showMenuBtn];
        [headerView addSubview:addCollectionBtn];
        [headerLab release];
        [headerImageView release];

        [backGroundImageView release];
        return headerView;
    }
    return nil;
}

-(IBAction)showShopMenuAction:(id)sender {
    //创建新的试图并将试图推入栈中
    ShopMenuController *shopMenuController = [[ShopMenuController alloc]initWithNibName:@"ShopMenuController" bundle:nil];
    UINavigationController  *theNavController = [[UINavigationController alloc]
                                                 initWithRootViewController:shopMenuController];
    [shopMenuController release];
    theNavController.navigationBar.tintColor = [UIColor blackColor];
    [self.navigationController pushViewController: shopMenuController animated:YES];
    [theNavController release];
    
}

//设置section的footer的高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if(section==0){
        return 5;    
    }
    else{
        return 80;
    }
}

//设置section的footer
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if(section == 1){
        
        UIView *footerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 60)] autorelease];
        //分享给朋友按钮
        UIButton *shareBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 
        [shareBtn setFrame:CGRectMake(17, 10, 111, 29.5)];
        [shareBtn setBackgroundImage:[UIImage imageNamed:@"Detail-031.png"] forState:UIControlStateNormal];
        [shareBtn setBackgroundImage:[UIImage imageNamed:@"Detail-031.png"] forState:UIControlStateHighlighted];
        [shareBtn setTitle:@"分享给朋友" forState:UIControlStateNormal];
        shareBtn.titleLabel.font=[UIFont fontWithName:@"Helvetica-Bold" size:16];
        //信息报错按钮
        UIButton *errorBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 
        [errorBtn setFrame:CGRectMake(191.5, 10, 111, 29.5)];
        [errorBtn setBackgroundImage:[UIImage imageNamed:@"Detail-031.png"] forState:UIControlStateNormal];
        [errorBtn setBackgroundImage:[UIImage imageNamed:@"Detail-031.png"] forState:UIControlStateHighlighted];
        [errorBtn setTitle:@"信息报错" forState:UIControlStateNormal];
        errorBtn.titleLabel.font=[UIFont fontWithName:@"Helvetica-Bold" size:16];
        [footerView addSubview:shareBtn];
        [footerView addSubview:errorBtn];
        return footerView;
    }
    return nil;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload {
    [super viewDidUnload];
}

- (void)dealloc {
	[firstSectionArray release];
    [secondSectionArray release];
    if (listData != nil) {
        [listData release];
    }
    [supplier release];
    [super dealloc];
}
@end
